package com.minimalist.basic.controller.lindi;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.news.LindiNewsQueryVO;
import com.minimalist.common.entity.lindi.vo.news.LindiNewsVO;
import com.minimalist.common.feign.client.lindi.LindiNewsFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "林地指标交易平台_新闻管理")
@RequestMapping("/basic/lindi/news")
public class LindiNewsController{

    @Autowired
    private LindiNewsFeignClient feignClient;

    @PostMapping("/addNews")
    @SaCheckPermission("lindi:news:add")
    @Operation(summary = "添加新闻")
    public R<?> addNews(
            @RequestBody @Validated(Add.class) LindiNewsVO newsVO){
        newsVO.setCreateId(StpUtil.getLoginIdAsLong());
        newsVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        newsVO.setCreateTime(now);
        newsVO.setUpdateTime(now);
        return feignClient.addNews(newsVO);
    }

    @DeleteMapping("/deleteNewsByNewsId")
    @SaCheckPermission("lindi:news:delete")
    @Operation(summary = "删除新闻 -> 根据新闻ID删除")
    public R<?> deleteNewsByNewsId(
            @RequestParam("NewsId")
            @NotNull(message = "新闻ID不能为空")
            @Parameter(name = "NewsId", required = true
                    , description = "新闻ID") Long newsId){
        return feignClient.deleteNewsByNewsId(newsId);
    }

    @PutMapping("/updateNewsByNewsId")
    @SaCheckPermission("lindi:news:update")
    @Operation(summary = "修改新闻")
    public R<?> updateNewsByNewsId(
            @RequestBody @Validated(Update.class) LindiNewsVO newsVO){
        newsVO.setCreateId(StpUtil.getLoginIdAsLong());
        newsVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        newsVO.setCreateTime(now);
        newsVO.setUpdateTime(now);
        return feignClient.updateNewsByNewsId(newsVO);
    }

    @GetMapping("/getPageNewsList")
    @SaCheckPermission("lindi:news:get")
    @Operation(summary = "查询新闻列表(分页)")
    public R<PageResp<LindiNewsVO>> getPageNewsList(LindiNewsQueryVO queryVO){
        System.out.println("queryVO = " + queryVO);
        System.out.println("feignClient.getPageNewsList(queryVO) = " + feignClient.getPageNewsList(queryVO));
        return feignClient.getPageNewsList(queryVO);
    }

    @GetMapping("/getNewsByNewsId/{NewsId}")
    @SaCheckPermission("lindi:news:get")
    @Operation(summary = "根据新闻ID查询新闻")
    public R<LindiNewsVO> getNewsByNewsId(
            @PathVariable(value = "NewsId")
            @NotNull(message = "新闻ID不能为空")
            @Parameter(name = "NewsId", description = "参数ID"
                    , required = true) Long newsId){
        return feignClient.getNewsByNewsId(newsId);
    }
}